<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Hecore
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: ModuleController.php 2010-07-02 19:52 ermek $
 * @author     Ermek
 */

/**
 * @category   Application_Extensions
 * @package    Hecore
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Hecore_ModuleController extends Core_Controller_Action_Admin {
	public function init() {
		$ajaxContext = $this->_helper->getHelper ( 'AjaxContext' );
		$ajaxContext->addActionContext ( 'license', 'json' )->addActionContext ( 'edit', 'json' )->initContext ();
	}
	
	public function licenseAction() {
		$hecoreApi = Engine_Api::_ ()->getApi ( 'core', 'hecore' );
		
		$this->view->product = $product = $this->_getParam ( 'name', null );
		$this->view->version = $version = $this->_getParam ( 'version', null );
		$this->view->target_version = $target_version = $this->_getParam ( 'target_version' );
		
		$this->view->form = $form = new Hecore_Form_License ();
		
		$modulesTable = Engine_Api::_ ()->getDbTable ( 'modules', 'hecore' );
		$module = $modulesTable->findByName ( $product );
		
		if (! $module) {
			$module = $modulesTable->createRow ();
			$module_arr = array ('name' => $product, 'version' => $version, 'key' => '', 'installed' => 0 );
			
			$module->setFromArray ( $module_arr );
			$module->save ();
		}
		
		$module_arr = $module->toArray ();
		
		if (! $this->getRequest ()->isPost ()) {
			$form->name->setValue ( $module_arr ['name'] );
			$form->version->setValue ( $module_arr ['version'] );
			
			return;
		}
		
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			return;
		}
		
		// Process
		$values = $form->getValues ();
		$result = false;
		$error_message = '';
		
		$parameters = array ('task' => 'install', 'license' => $values ['license'], 'product' => $values ['name'], 'domain' => $_SERVER ['HTTP_HOST'] );
		
		if ($target_version) {
			
			if (version_compare ( $version, $target_version ) === 0) {
				$parameters ['task'] = 'check_refresh';
			} else {
				$parameters ['task'] = 'check_upgrade';
			}
		}
		
		$server_result = $hecoreApi->checkLicense ( $parameters );
		
		eval ( $server_result );
		
		if ($target_version) {
			if ($result) {
				$version = $target_version;
				$this->view->pluginInstalled = true;
			} else {
				$form->addError ( $error_message );
			}
		}
		
		if (isset ( $this->view->pluginInstalled ) && $this->view->pluginInstalled) {
			$module->setFromArray ( array ('version' => $version, 'key' => $values ['license'], 'installed' => 1 ) );
			
			$module->save ();
		}
	}
	
	public function editAction() {
		$hecoreApi = Engine_Api::_ ()->getApi ( 'core', 'hecore' );
		$this->view->product = $product = $this->_getParam ( 'name', null );
		
		$this->view->form = $form = new Hecore_Form_License ();
		
		$modulesTable = Engine_Api::_ ()->getDbTable ( 'modules', 'hecore' );
		$module = $modulesTable->findByName ( $product );
		
		if (! $module) {
			$module = $modulesTable->createRow ();
			$module_arr = array ('name' => $product, 'version' => '4.0.0', 'key' => '', 'installed' => 0 );
			
			$module->setFromArray ( $module_arr );
			$module->save ();
		}
		
		$module_arr = $module->toArray ();
		
		if (! $this->getRequest ()->isPost ()) {
			$form->name->setValue ( $module_arr ['name'] );
			$form->version->setValue ( $module_arr ['version'] );
			
			return;
		}
		
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			return;
		}
		
		// Process
		$values = $form->getValues ();
		
		$parameters = array ('task' => 'update', 'license' => $values ['license'], 'product' => $values ['name'], 'domain' => $_SERVER ['HTTP_HOST'] );
		
		$result = $hecoreApi->checkLicense ( $parameters );
		eval ( $result );
		
		if (isset ( $this->view->keyUpdated ) && $this->view->keyUpdated) {
			$module->setFromArray ( array ('version' => $values ['version'], 'key' => $values ['license'], 'installed' => 1 ) );
			
			$module->save ();
		}
	}
}